import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getTagAllApi } from '@/api/tag/tag'
import { Message } from '@arco-design/web-vue'

export const useTagStore = defineStore('tag', () => {
  const tagList = ref<API.Tag[]>([])
  const loading = ref(false)

  // 获取所有标签
  const fetchAllTags = async () => {
    if (tagList.value.length > 0) {
      return tagList.value
    }
    
    loading.value = true
    try {
      const res = await getTagAllApi()
      if (res.code === 0 && res.data) {
        tagList.value = res.data
        return res.data
      } else {
        Message.error(res.message || '获取标签列表失败')
        return []
      }
    } catch (error) {
      console.error('获取标签列表失败', error)
      Message.error('获取标签列表失败')
      return []
    } finally {
      loading.value = false
    }
  }

  // 根据ID获取标签名称
  const getTagNameById = (id: number) => {
    const tag = tagList.value.find(tag => tag.id === id)
    return tag?.name || `标签${id}`
  }

  return {
    tagList,
    loading,
    fetchAllTags,
    getTagNameById
  }
}) 